package com.itheima.socket.udp.demo04;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveDemo04 {

    public static void main(String[] args) throws IOException {


            MulticastSocket multicastSocket = new MulticastSocket(6666);
            InetAddress inetAddress = InetAddress.getByName("224.0.0.1");
            multicastSocket.joinGroup(inetAddress);
        while (true) {
            byte[] bytes = new byte[1024];
            int length = bytes.length;
            DatagramPacket datagramPacket = new DatagramPacket(bytes, length);
            multicastSocket.receive(datagramPacket);
            byte[] data = datagramPacket.getData();
            int length1 = data.length;
            System.out.println(new String(bytes, 0, length1));
            multicastSocket.leaveGroup(inetAddress);
//            multicastSocket.close();
        }
    }
}
